package cc.vicp.djx314.msing.util.web.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cc.vicp.djx314.msing.util.web.CommonUtil;
import cc.vicp.djx314.msing.util.web.FileOutPutUtil;

@Deprecated
public class FileOutPutUtilImpl extends FileOutPutUtil {
	public static final Logger logger = LoggerFactory.getLogger(FileOutPutUtilImpl.class);

	@Override
	public void fileOutPut(HttpServletRequest request,
			HttpServletResponse response, File file, String browserType) {
		if (browserType.equals(CommonUtil.FIREFOX)) {
			fireFoxFileOutPut(request, response, file);
		} else if (browserType.equals(CommonUtil.CHROME)) {
			chromeFileOutPut(request, response, file);
		}
	}

	protected void fireFoxFileOutPut(HttpServletRequest request, HttpServletResponse response, File file) {
		response.reset();

		//response.setDateHeader("Date", new Date().getTime());

		logger.info("========================");

		long pos = 0;
		OutputStream os = null;
		FileInputStream is = null;
		try {
			is = new FileInputStream(file);
			long fSize = file.length();
			byte xx[] = new byte[4096];

			response.setContentType(CommonUtil.setContentType(file.getName()));

			response.setHeader("Accept-Ranges", "bytes");
			response.setDateHeader("Date", new Date().getTime());
			response.setHeader("Content-Length", fSize + "");

			if (request.getHeader("Range") != null) {
				// 若客户端传来Range，说明之前下载了一部分，设置206状态(SC_PARTIAL_CONTENT)
				response.setStatus(HttpServletResponse.SC_PARTIAL_CONTENT);
				pos = Long.parseLong(request.getHeader("Range")
						.replaceAll("bytes=", "").replaceAll("-", ""));
			}
			logger.info("=1=1=1=1=1={}=1=1=1=1=1=", pos);
			if (pos != 0) {
				String contentRange = new StringBuffer("bytes ")
				.append(new Long(pos).toString()).append("-")
				.append(new Long(fSize - 1).toString()).append("/")
				.append(new Long(fSize).toString()).toString();
				response.setHeader("Content-Range", contentRange);
				//System.out.println("Content-Range=" + contentRange);
				// 略过已经传输过的字节
				is.skip(pos);
			}
			os = response.getOutputStream();
			boolean all = false;
			while (!all) {
				int n = is.read(xx);
				if (n != -1) {
					os.write(xx, 0, n);
					if (n != 4096) {
						logger.info(n + "");
					}
				} else {
					all = true;
					logger.info("*******************");
				}
			}
			os.flush();
		} catch (IOException e) {
			e.printStackTrace();
			return;
		} finally {
			try {
				if (is != null)
					is.close();
				if (os != null)
					os.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	protected void chromeFileOutPut(HttpServletRequest request, HttpServletResponse response, File file) {
		response.reset();

		OutputStream os = null;
		FileInputStream is = null;
		try {
			System.out.println("22");
			is = new FileInputStream(file);
			long fSize = file.length();
			byte xx[] = new byte[4096];

			response.setContentType(CommonUtil.setContentType(file.getName()));

			response.setDateHeader("Date", new Date().getTime());
			response.setHeader("Accept-Ranges", "bytes");
			response.setHeader("Content-Length", fSize + "");

			os = response.getOutputStream();
			boolean all = false;
			while (!all) {
				int n = is.read(xx);
				if (n != -1) {
					os.write(xx, 0, n);
				} else {
					all = true;
				}
			}
			os.flush();

		} catch (IOException e) {
			return;
		} finally {
			try {
				if (is != null)
					is.close();
				if (os != null)
					os.close();
			} catch (IOException e) {
			}
		}
	}
}